package com.example.administrator.printer.module.feie;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.printer.base.BaseActivity;
import com.example.administrator.printer.R;
import com.example.cloudprinter.printer.feie.FeiePrinter;
import com.example.cloudprinter.printer.feie.callback.OnAddCallback;
import com.example.cloudprinter.printer.feie.callback.OnDeleteCallback;
import com.example.cloudprinter.printer.feie.callback.OnPrintCallback;
import com.example.cloudprinter.printer.feie.callback.OnStateCallback;
import com.example.cloudprinter.printer.feie.format.FeieFormat;

public class FeieActivity extends BaseActivity implements View.OnClickListener {
    EditText et_number, et_key, et_alias, et_content;
    Button btn_auth, btn_delete, btn_print, btn_getState;
    TextView tv_state;

    FeiePrinter printer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feie);

        et_number = findViewById(R.id.et_number);
        et_key = findViewById(R.id.et_key);
        et_alias = findViewById(R.id.et_alias);
        et_content = findViewById(R.id.et_content);
        btn_auth = findViewById(R.id.btn_auth);
        btn_delete = findViewById(R.id.btn_delete);
        btn_print = findViewById(R.id.btn_print);
        btn_getState = findViewById(R.id.btn_get_state);
        tv_state = findViewById(R.id.tv_state);
        btn_auth.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_print.setOnClickListener(this);
        btn_getState.setOnClickListener(this);

        printer = new FeiePrinter();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_auth://添加打印机
                printer.addPrinter(et_number.getText().toString(), et_key.getText().toString(),
                        et_alias.getText().toString(), new OnAddCallback() {

                            @Override
                            public void onAddSuccess() {
                                Toast.makeText(getApplication(), "添加打印机成功", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onAddFailure(int errNo, String errMsg) {
                                Toast.makeText(getApplication(), "添加打印机失败，原因：" + errMsg, Toast.LENGTH_SHORT).show();
                            }

                        });
                break;
            case R.id.btn_delete://删除打印机
                printer.deletePrinter(et_number.getText().toString(), new OnDeleteCallback() {

                    @Override
                    public void onDeleteSuccess() {
                        tv_state.setText("");
                        Toast.makeText(getApplication(), "删除打印机成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onDeleteFailure(int errNo, String errMsg) {
                        Toast.makeText(getApplication(), "删除打印机失败，原因：" + errMsg, Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.btn_get_state://获取打印机状态
                printer.getPrinterState(et_number.getText().toString(), new OnStateCallback() {

                    @Override
                    public void onState(String state) {
                        tv_state.setText("打印机状态：" + state);
                    }

                    @Override
                    public void onQuestFailure(int errNo, String errMsg) {
                        Toast.makeText(getApplication(), "获取状态失败，原因：" + errMsg, Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.btn_print://打印
                printer.print(et_number.getText().toString(), order.getPrintTemplate(new FeieFormat()), "1", new OnPrintCallback() {

                    @Override
                    public void onPrintSuccess(String content) {
                        Toast.makeText(getApplication(), "开始请求打印", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onPrintFailure(int errNo, String errMsg) {
                        Toast.makeText(getApplication(), "获取状态失败，原因：" + errMsg, Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (printer != null)
            printer.release();
    }
}
